﻿using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Utilities.Encoders;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace com.lythen.file.Security
{
    /***************************************************************************
     * CLR版本：4.0.30319.42000
     *唯一标识：8f650ac8-16e1-40ed-8e69-3fb07c728fb2
     *  创建人：赖仁良
     *创建时间：2024/1/2 9:58:44
     *功能描述：
     * ----------------------------------------------------------------
     * 修改人：
     * 时间：
     * 修改说明：
     *=========================================================================*/
    /// <summary>
    /// 
    /// </summary>
    public class KeyHelper
    {
        private string privateKey;
        private string publicKey;

        private AsymmetricKeyParameter privateKeyParam;
        private AsymmetricKeyParameter publicKeyParam;

        public static KeyHelper GetInstance(string privateKey,string publicKey)
        {
            return new KeyHelper(privateKey, publicKey);
        }
        private KeyHelper(string privateKey, string publicKey)
        {
            this.privateKey = privateKey;
            this.publicKey = publicKey; 
            BigInteger d = new BigInteger(this.privateKey, 16);
            
            this.privateKeyParam = GmUtil.GetPrivatekeyFromD(d);

            if (this.publicKey.Length == 130)
            {
                this.publicKey = this.publicKey.Substring(2, 128);
            }
            //公钥X，前64位
            String x = this.publicKey.Substring(0, 64);
            //公钥Y，后64位
            String y = this.publicKey.Substring(64);
            //获取公钥对象

            this.publicKeyParam = GmUtil.GetPublickeyFromXY(new BigInteger(x, 16), new BigInteger(y, 16));
        }
        /// <summary>
        /// 保存自己的公钥
        /// </summary>
        /// <param name="fileName"></param>
        public void ExportPublicKey(string fileName)
        {
            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }
            using(FileStream fileStream = new FileStream(fileName, FileMode.CreateNew, FileAccess.Write))
            {
                using(StreamWriter streamWriter = new StreamWriter(fileStream, Encoding.UTF8))
                {
                    streamWriter.WriteLine(this.publicKey);
                }
            }
        }
        /// <summary>
        /// 生成16进制密钥
        /// </summary>
        /// <returns></returns>
        public (string publicKey,string privateKey) CreateStringKeys()
        {

            var keys = CreateBytesKeys();
            return (Hex.ToHexString(keys.publicKey), Hex.ToHexString(keys.privateKey));
        }
        /// <summary>
        /// 生成byte[]密钥
        /// </summary>
        /// <returns></returns>
        public (byte[] publicKey,byte[] privateKey) CreateBytesKeys()
        {
            var keyPair = GmUtil.GenerateKeyPair();

            ECPrivateKeyParameters ecpriv = (ECPrivateKeyParameters)keyPair.Private;
            ECPublicKeyParameters ecpub = (ECPublicKeyParameters)keyPair.Public;
            BigInteger privateKey = ecpriv.D;
            Org.BouncyCastle.Math.EC.ECPoint publicKey = ecpub.Q;
            return (publicKey.GetEncoded(), privateKey.ToByteArray());
        }
        
    }
}
